from OpenSSL import crypto
# pfx_file_path = r"C:\Users\ljt\Desktop\work\zhong\\privateKey.p12"
pfx_file_path = r'/Users/jingyugao/PycharmProjects/DjangoAPI/backend/encryption/channPri.p12'

#book = xlrd.open_workbook(r'backend/excel_value/shuju.xlsx')
#/Users/jingyugao/PycharmProjects/DjangoAPI/backend/encryption

# open it, using password. Supply/read your own from stdin.
p12 = crypto.load_pkcs12(open(pfx_file_path, 'rb').read(), '123456')
cer = p12.get_certificate()     # (signed) certificate object
pkey = p12.get_privatekey()      # private key.
ca_cer = p12.get_ca_certificates() # ca chain.
print(cer, pkey, ca_cer)
print('版本', cer.get_version())
print('签名算法', cer.get_signature_algorithm())
print('序列号：', cer.get_serial_number())
print('证书是否过期：', cer.has_expired())
print('在此之前无效：', cer.get_notBefore())
print('在此之后无效', cer.get_notAfter())
#主题名称
subject = cer.get_subject()
s_components = subject.get_components()
print(s_components)
key_dict = {'UID': '用户 ID',
			'CN': '常用名称',
			'OU': '组织单位',
			'O': '组织',
			'C': '国家或地区'
			}
for (key, value) in s_components:
	print(key, value)

	print(key_dict.get(key.decode(), key))
#签发者名称
suer = cer.get_issuer()
print(suer.get_components())
#证书扩展信息
print('扩展数：', cer.get_extension_count())
# print('扩展1：', cer.get_extension(0))